package com.fanbuchi.app.ins.net;

import com.squareup.okhttp.Authenticator;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.net.Proxy;

/**
 * Created by os on 16/5/12.
 * Title
 *
 * client.setAuthenticator(new Authenticator() {
 public Request authenticate(Proxy proxy, Response response) throws IOException {
 String credential = Credentials.basic("user", "password");
 return response.request().newBuilder()
 .header("Authorization", credential)
 .build();
 }

 public Request authenticateProxy(Proxy proxy, Response response)
 throws IOException {
 return null;
 }
 });

 */
public class BasicAuthConfig {

    public static void basicAuthConfig(OkHttpClient mOkHttpClient){
        if (mOkHttpClient==null)return;
        //1.0
        //mOkHttpClient.setAuthenticator(AuthenticatorAdapter.INSTANCE);
        //2.0
        mOkHttpClient.interceptors().add(new BasicAuthorizationInterceptor());
        //3.0
        /*mOkHttpClient.setAuthenticator(new Authenticator() {
            @Override
            public Request authenticate(Proxy proxy, Response response) throws IOException {
                String credential = Credentials.basic("name", "password");
                return response.request().newBuilder().header("Authorization", credential).build();
            }

            @Override
            public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
                return null;
            }
        });*/
    }
}
